package com.example.zbh.dsliao.model;

import android.util.Log;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.db.DatabaseHelper;
import com.example.zbh.dsliao.db.entity.UserContact;
import com.example.zbh.dsliao.model.entity.ContactPeople;
import com.example.zbh.dsliao.model.modelinterface.ModelContactInterface;
import com.example.zbh.dsliao.presenter.presenterinterface.PresenterContactInterface;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zbh on 15/11/20.
 */
public class ContactModel implements ModelContactInterface{
    private List<String> list = new ArrayList<>();
    private List<String> blackList = new ArrayList<>();
    private List<ContactPeople> peoples = new ArrayList<>();

    @Override
    public void recevieveContact(PresenterContactInterface presenterContactInterface) {
        Log.i("nan",3 + "");
        try {
            list = EMContactManager.getInstance().getContactUserNames();
            blackList = EMContactManager.getInstance().getBlackListUsernames();
            Log.i("aaaaaaa",list.toString());
            if (blackList != null && blackList.size() > 0){
                for (int i = 0; i < blackList.size(); i++) {
                    list.remove(blackList.get(i));
                }
            }
            for (int i = 0; i < list.size(); i++) {
                UserContact userContact = DatabaseHelper.getInstance().queryContactPeopleByName(list.get(i));
                if (userContact != null){
                    String nickName = userContact.getNickname();
                    if (nickName != null){
                        list.remove(list.get(i));
                        list.add(nickName);
                    }
                }
            }

            for (int i = 0; i < list.size(); i++) {
                peoples.add(new ContactPeople(list.get(i)));
                Log.i("aaaaaaa",peoples.toString());
            }
            Collections.sort(peoples);

            presenterContactInterface.sendReciveResult(peoples);

        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }
}
